import numpy as np
import time
import matplotlib.pyplot as plt

sizes = [100, 500, 1000, 2000, 5000, 10000, 20000]  # 测试矩阵尺寸
times = []

for n in sizes:
    # 生成随机矩阵（条件数适中）
    A = np.random.rand(n, n)
    A = A + A.T  # 对称矩阵
    A += n * np.eye(n)  # 确保正定

    b = np.random.rand(n)

    start_time = time.time()
    x = np.linalg.solve(A, b)
    end_time = time.time()

    times.append(end_time - start_time)
    print(f"Size: {n}x{n}, Time: {end_time - start_time:.4f} seconds")

# 绘制性能曲线
plt.plot(sizes, times, 'o-')
plt.xlabel('Matrix Size (n)')
plt.ylabel('Solve Time (seconds)') 
plt.title('NumPy Linear Solver Performance')
plt.grid(True)
plt.show()
